﻿using MvcApplication.Common.Data.Mapping.Auth;
using MvcApplication.Entity.Auth;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvcApplication.Common.Data.Context
{
    public class AuthContext : DbContext
    {
        static AuthContext()
        {
            Database.SetInitializer<AuthContext>(null);
        }

        public AuthContext()
            : base("name=AuthContext")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }

        public AuthContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }

        public AuthContext(string nameOrConnectionString, DbCompiledModel model)
            : base(nameOrConnectionString, model)
        {
        }

        public AuthContext(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {
        }

        public AuthContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
            : base(existingConnection, model, contextOwnsConnection)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CategoryMap());
            modelBuilder.Configurations.Add(new DicDetailMap());
            modelBuilder.Configurations.Add(new DicIndexMap());
            modelBuilder.Configurations.Add(new CommonApplyMap());
            modelBuilder.Configurations.Add(new ModuleMap());
            modelBuilder.Configurations.Add(new ModuleElementMap());
            modelBuilder.Configurations.Add(new OrgMap());
            modelBuilder.Configurations.Add(new RelevanceMap());
            modelBuilder.Configurations.Add(new ResourceMap());
            modelBuilder.Configurations.Add(new RoleMap());
            modelBuilder.Configurations.Add(new StockMap());
            modelBuilder.Configurations.Add(new UserMap());
            modelBuilder.Configurations.Add(new ApplyTransitionHistoryMap());
        }

        #region 文章
        public DbSet<Category> Categories { get; set; }
        public DbSet<DicDetail> DicDetails { get; set; }
        public DbSet<DicIndex> DicIndices { get; set; }
        public DbSet<CommonApply> GoodsApplies { get; set; }
        public DbSet<Module> Modules { get; set; }
        public DbSet<ModuleElement> ModuleElements { get; set; }
        public DbSet<Org> Orgs { get; set; }
        public DbSet<Relevance> Relevances { get; set; }
        public DbSet<Resource> Resources { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Stock> Stocks { get; set; }
        public DbSet<User> Users { get; set; }

        public DbSet<ApplyTransitionHistory> ApplyTransitionHistories { get; set; }

        #endregion

        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
            catch (DBConcurrencyException ex)
            {
                throw ex;
            }
            catch (DbUpdateException ex)
            {
                throw ex;
            }
        }
    }
}
